﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Defect_Tracking_System.Models;
using Defect_Tracking_System.Helpers;

namespace Defect_Tracking_System.Controllers
{
    public class ClientController : Controller
    {
        AccountManagement accountManagement = AccountManagement.GetInstance();
        ClientManagement clientManagement = ClientManagement.GetInstance();
        //
        // GET: /Client/

        [Authorize(Roles = "Group Admin")]
        public ActionResult Index(int PageIndex = 1)
        {

            string GroupId = accountManagement.GetUserByUserName(User.Identity.Name).GroupId.ToString();

            PaginatedList<Client> Model = new PaginatedList<Client>(clientManagement.GetClients(GroupId), PageIndex, 10);

            return View(Model);
        }

        [Authorize(Roles="Group Admin")]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost, Authorize(Roles="Group Admin")]
        public ActionResult Add(ClientModel model)
        {
            if (ModelState.IsValid)
            {
                model.GroupId = accountManagement.GetUserByUserName(User.Identity.Name).Group.GroupId.ToString();

                try
                {
                    clientManagement.AddClient(model);
                    ViewData["message"] = "Add Successfully";
                }
                catch (Exception ex)
                {
                    ViewData["message"] = ex.Message;
                }
            }
            return View(model);
        }

        [Authorize(Roles = "Group Admin")]
        public ActionResult Edit(String ClientId)
        {
            Client client = clientManagement.GetClient(ClientId);

            if(client == null)
            {
                ViewData["Message"] = "Client is not existed";
                return View("Error");
            }

            ClientModel Model = clientManagement.GetClientModel(ClientId);

            return View(Model);
        }

        [Authorize(Roles = "Group Admin")]
        public ActionResult Delete(String ClientId, String ReturnUrl)
        {
            Client client = clientManagement.GetClient(ClientId);

            if (client == null)
            {
                Session["Message"] = "Client is not existed";
            }
            try
            {
                clientManagement.DeleteClient(client);
                Session["Message"] = "Delete client successful";
            }
            catch (Exception ex)
            {
                Session["Message"] = ex.Message;
            }

            return Redirect(ReturnUrl);
        }

        [HttpPost, Authorize(Roles = "Group Admin")]
        public ActionResult Edit(String ClientId, ClientModel Model)
        {
            Client client = clientManagement.GetClient(ClientId);

            if (client == null)
            {
                ViewData["Message"] = "Client is not existed";
                return View("Error");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    clientManagement.EditClient(Model);
                    ViewData["message"] = "Edit Successfully";
                }
                catch (Exception ex)
                {
                    ViewData["message"] = ex.Message;
                }
            }
            return View(Model);
        }

        [HttpPost, Authorize]
        public ActionResult Search(FormCollection Forms)
        {
            String Keyword = Forms["Keyword"];
            String ReturnType = Forms["ReturnType"];

            var Clients = clientManagement.Search(Keyword, User.Identity.Name);

            if (ReturnType == "json")
            {
                List<Object> objects = new List<object>();

                foreach (Client c in Clients.ToList())
                {
                    objects.Add(new { Id = c.ClientId, FirstName = c.FirstName, LastName = c.LastName, Company = c.Company, Email = c.Email });
                }

                return Json(objects);
            }
            else
            {
                return View();
            }
        }
    }
}
